//
//  CodeController02.m
//  PosApp
//
//  Created by 李英新 on 2018/10/3.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "CodeController02.h"
#import "MyCodeViewCell.h"
#import "CodeModel.h"
#import "NoOrderView.h"

@interface CodeController02 ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *CodeTable02;
@property (strong, nonatomic) NSMutableArray * codeArr02;
@property (assign, nonatomic) NSUInteger page;
@property (strong, nonatomic) MBProgressHUD * hud;

@end

@implementation CodeController02

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.codeArr02.count == 0) {
        [self.CodeTable02.mj_header beginRefreshing];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.CodeTable02.delegate = self;
    self.CodeTable02.dataSource = self;
    self.CodeTable02.rowHeight = 130;
    self.CodeTable02.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.CodeTable02 registerNib:[UINib nibWithNibName:@"MyCodeViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.CodeTable02.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.codeArr02 removeAllObjects];
        [self UpLoadDataOfCodeController02];
    }];
    
    
    self.CodeTable02.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page = self.page +1;
        [self UpLoadDataOfCodeController02];
    }];
    
    
}
- (void)UpLoadDataOfCodeController02{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [dic setObject:[NSString stringWithFormat:@"%ld",self.page] forKey:@"page"];
    [dic setObject:@"1" forKey:@"state"];
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"list" andAction:@"Mypoints" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"积分订单请求的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                CodeModel * model = [CodeModel mj_objectWithKeyValues:dic];
                [self.codeArr02 addObject:model];
            }
            [self.CodeTable02 reloadData];
        }
        
        [self.CodeTable02.mj_header endRefreshing];
        [self.CodeTable02.mj_footer endRefreshing];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
        [self.CodeTable02.mj_header endRefreshing];
        [self.CodeTable02.mj_footer endRefreshing];
        [self.hud hideAnimated:YES];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.codeArr02.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCodeViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[MyCodeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.CellType = 2;
    cell.tag = indexPath.row;
    [cell CreateMyCodeViewCell];
    
    CodeModel * model = self.codeArr02[indexPath.row];
    cell.model = model;
    
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.codeArr02.count == 0) {
        return self.CodeTable02.bounds.size.height;
    }else{
        return 10;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NoOrderView * headV = [[NSBundle mainBundle] loadNibNamed:@"NoOrderView" owner:self options:nil][0];
    if (self.codeArr02.count == 0) {
        headV.BackView.hidden = NO;
    }else{
        headV.BackView.hidden = YES;
    }
    return headV;
}





- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView * foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 8)];
    foot.backgroundColor = [UIColor clearColor];
    return foot;
}

- (NSMutableArray *)codeArr02 {
    if (!_codeArr02) {
        _codeArr02 = [NSMutableArray array];
    }
    return _codeArr02;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
